package io.renren.admin;

import com.xiaoleilu.hutool.util.StrUtil;
import io.renren.entity.WordEntity;
import io.renren.service.WordService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import io.renren.utils.thirdparty.YouDaoDict;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @date 2017-05-22 16:03:39
 */
@RestController
@RequestMapping("word")
public class WordController {
	@Autowired
	private WordService wordService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("word:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<WordEntity> wordList = wordService.queryList(query);
		int total = wordService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(wordList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("word:info")
	public R info(@PathVariable("id") Long id){
		WordEntity word = wordService.selectOne(id);
		if (StrUtil.isEmpty(word.getChinese())){
			String chinese=YouDaoDict.translation(word.getWord());
			word.setChinese(chinese);
			wordService.update(word);
		}
		return R.ok().put("word", word);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("word:save")
	public R save(@RequestBody WordEntity word){
		wordService.insert(word);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("word:update")
	public R update(@RequestBody WordEntity word){
		wordService.update(word);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("word:delete")
	public R delete(@RequestBody Long[] ids){
		wordService.deleteBatch(ids);

		return R.ok();
	}

}
